Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable colours on GitHub Actions by default #7462

Closed
wants to merge 5 commits into from
Closed

Enable colours on GitHub Actions by default #7462

wants to merge 5 commits into from

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Jul 8, 2020

Workaround to fix #7443.

GitHub Actions doesn't provide a tty, so many tools that normally output in colour instead output in monochrome: actions/runner#241.

GitHub say it's low in their backlog and they have no plans to fix it (actions/runner#241 (comment)), so instead of requiring every pytest user to explicitly force colours for pytest, improve the pytest colour detection using the GITHUB_ACTIONS env var:

GITHUB_ACTIONS Always set to true when GitHub Actions is running the workflow. You can use this variable to differentiate when tests are being run locally or by GitHub Actions.

https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables


Test using this branch:

image

Copy link
Member

@The-Compiler The-Compiler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me, especially since we handle some special cases there already anyways (e.g. disabling markup with Jython on Windows).

Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

BTW the java condition is no longer needed as we don't support Jython (Python2 only AFAIK), so can remove it as well if you feel like it.

@@ -32,7 +32,7 @@ def should_do_markup(file: TextIO) -> bool:
and file.isatty()
and os.environ.get("TERM") != "dumb"
and not (sys.platform.startswith("java") and os._name == "nt")
)
) or os.environ.get("GITHUB_ACTIONS") == "true"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be a little clearer as a separate if before the return (can leave as is if you prefer)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea, since we then handle all the special cases before the "ordinary" one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved!

@@ -32,7 +32,7 @@ def should_do_markup(file: TextIO) -> bool:
and file.isatty()
and os.environ.get("TERM") != "dumb"
and not (sys.platform.startswith("java") and os._name == "nt")
)
) or os.environ.get("GITHUB_ACTIONS") == "true"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea, since we then handle all the special cases before the "ordinary" one.

@@ -32,7 +32,7 @@ def should_do_markup(file: TextIO) -> bool:
and file.isatty()
and os.environ.get("TERM") != "dumb"
and not (sys.platform.startswith("java") and os._name == "nt")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me to remove this if we don't support Jython anymore - looks like it was originally added in pytest-dev/py@536252c some 10 years ago 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed!

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks!

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I'm actually -1 on this. the environment variable doesn't tell whether or not you're piped or other possible things where color is inappropriate and it's extremely special cased for github actions

@nicoddemus
Copy link
Member

the environment variable doesn't tell whether or not you're piped or other possible things where color is inappropriate

But if you are piped you don't get a atty, right? We should then adapt the check to take that in account instead of forcing it everytime.

Your point about being specific is correct, but if we can get this correct in all cases I still see it as a win as every user will get this for free without additional configuration.

@asottile
Copy link
Member

asottile commented Jul 8, 2020

the environment variable doesn't tell whether or not you're piped or other possible things where color is inappropriate

But if you are piped you don't get a atty, right? We should then adapt the check to take that in account instead of forcing it everytime.

Your point about being specific is correct, but if we can get this correct in all cases I still see it as a win as every user will get this for free without additional configuration.

right, but if you're running in github actions, you can't discern whether that output is going to the log or is being piped into something else since both cases would be (GITHUB_ACTIONS present, no tty)

@The-Compiler
Copy link
Member

This is a valid concern I'm afraid... and I can't think of any way to find out whether we're writing to a pipe/file or to the standard output on GitHub Actions. On the other hand, I wonder how common the case of someone piping pytest output somewhere on GitHub Actions would be.

Alternatively, maybe this should go into some pytest-github-actions plugin I guess? I've been toying with the idea of writing such a plugin to do output grouping similar to pytest-travis-fold, but as usual, haven't found the time yet 😆

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, but if you're running in github actions, you can't discern whether that output is going to the log or is being piped into something else since both cases would be (GITHUB_ACTIONS present, no tty)

Ahh got it, while running in GitHub actions we never have an tty, because we are being piped, of course. Thanks for the clarification.

In that case I agree we should probably turn down this PR.

Perhaps we should add a blurb somewhere in the docs mentioning --color=yes when running in CI services such as GHA, Travis, AppVeyor, etc? I don't think we have anything in that sense currently.

@The-Compiler
Copy link
Member

I think this is only a problem with GHA, as others somehow provide a fake TTY. FWIW I ended up using this at the top of the workflow file:

env:
  PY_COLORS: "1"

that way, colors will be enabled for both pytest and tox, and it won't influence other places where commandline arguments are passed or handled.

@nicoddemus
Copy link
Member

Let's close this for now, thanks @hugovk for giving it a go anyway! 👍

@nicoddemus nicoddemus closed this Jul 10, 2020
@hugovk
Copy link
Member Author

hugovk commented Jul 10, 2020

Sure, no problem!

@hugovk hugovk deleted the gha-enable-colours branch July 10, 2020 11:54
@umarcor
Copy link

umarcor commented Jul 10, 2020

Alternatively, maybe this should go into some pytest-github-actions plugin I guess? I've been toying with the idea of writing such a plugin to do output grouping similar to pytest-travis-fold, but as usual, haven't found the time yet 😆

@The-Compiler, we are already doing that in https://github.com/ghdl/ghdl-cosim/blob/master/test.py#L25-L30. See output: https://github.com/ghdl/ghdl-cosim/runs/854521453?check_suite_focus=true#step:4:13. Unfortunately, there is an annoyance which I could not solve: when pytest is used to execute makefiles, everything is correct; but when executing other python scripts, the output/log is empty. Compare https://github.com/ghdl/ghdl-cosim/runs/854521453?check_suite_focus=true#step:4:174 and any other of the "Log" collapsible groups. That's the reason that has prevented me from "announcing" the solution; which is really simple and effective otherwise.

@The-Compiler
Copy link
Member

@umarcor Interesting! I have no idea why that is. FWIW what I'd be interested in is a solution collapsing the output of pytest's own test failure output.

@umarcor
Copy link

umarcor commented Jul 10, 2020

FWIW what I'd be interested in is a solution collapsing the output of pytest's own test failure output.

All you need to do is print ##[group] before and ##[endgroup]. However, be aware that nested groups are NOT supported.

@The-Compiler
Copy link
Member

All you need to do is print ##[group] before and ##[endgroup]. However, be aware that nested groups are NOT supported.

Note you're using an old/alternative/depecated? syntax there, that should probably be ::group::title and ::endgroup:: instead.

@umarcor
Copy link

umarcor commented Jul 10, 2020

Yes, you are correct. It's an old, probably deprecated, syntax. Sorry about that. I'll fix it on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No colors by default on GitHub Actions
6 participants